var ibMarketWageForm = {
	/*------------------------------*/
	/* Constructor 					*/
	init: function()
	{
		Debug.write("Initializing wageForm.js");
		
		document.observe("dom:loaded", function(){
			ibMarketWageForm.loadObserve();
			ibMarketWageForm.updateWage();
		});
	},
	loadObserve: function()
	{
		$('w_how').observe('change', this.changeValue);
		$('w_how').observe('keyup', this.changeValue);
		
		$('w_when').observe('keyup', this.changeValue);
		
		$('w_minute').observe('change', this.changeValue);
		$('w_minute').observe('keyup', this.changeValue);
		$('w_hour').observe('change', this.changeValue);
		$('w_hour').observe('keyup', this.changeValue);
		$('w_day').observe('change', this.changeValue);
		$('w_day').observe('keyup', this.changeValue);
		
		$('currentTimeButton').observe('click', this.setTime);
		
		if($('group_id') == null)
		{
			$('member_name').disable();
		}
		if($('member_name') == null)
		{
			$('group_id').disable();
		}
		if($('member_name') != null && $('group_id') != null)
		{
			this.autoComplete = new ipb.Autocomplete( $('member_name'), { multibox: false, url: acp.autocompleteUrl, templates: { wrap: acp.autocompleteWrap, item: acp.autocompleteItem } } );
		}
	},
	changeValue: function(event)
	{
		ibMarketWageForm.updateWage();
	},
	updateWage: function()
	{
		Debug.write("Updating wageForm for payment");
		
		if($F($('w_how')) == 1)
		{
			$('w_when').enable();
			$('currentTimeButton').enable();
			$('li_wage_when').show();
			
			$('wage_time_ques').innerHTML = ipb.lang['wage_time_1'];
			
			if(!$F($('w_when')))
			{
				$('paymentInfo').value = ipb.lang['bad_time_format'];
				return;
			}
			
			var interval = $F($('w_day')) * 86400 + $F($('w_hour')) * 3600 + $F($('w_minute')) * 60;
			
			if(interval > 0)
			{
				var dateTime = $F($('w_when')).split(" ");
				if(dateTime.length != 2)
				{
					$('paymentInfo').value = ipb.lang['bad_time_format'];
					return;
				}
				var dateArray = dateTime[0].split("/"); 
				if(dateArray.length != 3)
				{
					$('paymentInfo').value = ipb.lang['bad_time_format'];
					return;
				}
				dateArray[0] = parseInt(dateArray[0].replace(/^0+/,''));
				dateArray[1] = parseInt(dateArray[1].replace(/^0+/,''));
				dateArray[2] = parseInt(dateArray[2].replace(/^0+/,''));
				if(isNaN(dateArray[0]) || isNaN(dateArray[1]) || isNaN(dateArray[2]))
				{
					$('paymentInfo').value = ipb.lang['bad_time_format'];
					return;
				}
				var timeArray = dateTime[1].split(":");
				if(timeArray.length != 2)
				{
					$('paymentInfo').value = ipb.lang['bad_time_format'];
					return;
				}
				timeArray[0] = parseInt(timeArray[0],10);
				timeArray[1] = parseInt(timeArray[1],10);
				if(isNaN(timeArray[0]) || isNaN(timeArray[1]))
				{
					$('paymentInfo').value = ipb.lang['bad_time_format'];
					return;
				}
				
				var time = $A();
				if($F($('w_day')) == 1)
				{
					time[time.size()] = ipb.lang['1_day'];
				}
				else if($F($('w_day')) > 1)
				{
					time[time.size()] = ipb.lang['x_days'].sub("%s", $F($('w_day')));
				}
				if($F($('w_hour')) == 1)
				{
					time[time.size()] = ipb.lang['1_hour'];
				}
				else if($F($('w_hour')) > 1)
				{
					time[time.size()] = ipb.lang['x_hours'].sub("%s", $F($('w_hour')));
				}
				if($F($('w_minute')) == 1)
				{
					time[time.size()] = ipb.lang['1_minute'];
				}
				else if($F($('w_minute')) > 1)
				{
					time[time.size()] = ipb.lang['x_minutes'].sub("%s", $F($('w_minute')));
				}
				
				var currentDate = new Date();
				currentDate.setSeconds(0,0);
				var current = currentDate.getTime();
				var nextDate = new Date();
				nextDate.setUTCFullYear(dateArray[2],dateArray[0]-1,dateArray[1]);
				nextDate.setUTCHours(timeArray[0],timeArray[1],0,0);
				var nextTime = nextDate.getTime();
				var offset = nextTime % (interval * 1000); // in ms
				var intervals = Math.floor(current / (interval * 1000)) + 1;
				
				nextDate.setTime(intervals * interval * 1000 + offset);
				
				var minute = nextDate.getUTCMinutes();
				if(minute < 10)
				{
					minute = "0"+minute;
				}
				var when = (nextDate.getUTCMonth()+1)+"/"+nextDate.getUTCDate()+"/"+nextDate.getUTCFullYear()+" "+nextDate.getUTCHours()+":"+minute;
				
				var message = ipb.lang['wage_message_1'];
				message = message.sub("<#TIME#>",time.join(", "));
				message = message.sub("<#WHEN#>",when);
				
				$('paymentInfo').value = message;
				
				return;
			}
			else
			{
				var message = ipb.lang['wage_message_1_every_second'];
				
				$('paymentInfo').value = message;
				
				return;
			}
		}
		else if($F($('w_how')) == 2)
		{
			$('w_when').disable();
			$('currentTimeButton').disable();
			$('li_wage_when').hide();
			
			$('wage_time_ques').innerHTML = ipb.lang['wage_time_2'];
			
			if($F($('w_hour')) == 24)
			{
				$('w_day').value = 7;
			}
			
			if($F($('w_hour')) >= 24)
			{
				time = ipb.lang['every_day_every_hour_x_minute'];
				time = time.sub("%s",$F($('w_minute')));
			}
			else//if($F($('w_hour')) < 24)
			{
				var timeOfDay;
				if($F($('w_minute')) < 10)
				{
					timeOfDay = "0"+$F($('w_minute'));
				}
				else
				{
					timeOfDay = $F($('w_minute'));
				}
				
				if($F($('w_hour')) == 0)
				{
					timeOfDay = 12 + ":" + timeOfDay + "AM";
				}
				else if($F($('w_hour')) < 12)
				{
					timeOfDay = $F($('w_hour')) + ":" + timeOfDay + "AM";
				}
				else if($F($('w_hour')) == 12)
				{
					timeOfDay = $F($('w_hour')) + ":" + timeOfDay + "PM";
				}
				else//if($F($('w_hour')) < 23)
				{
					timeOfDay = ($F($('w_hour'))-12) + ":" + timeOfDay + "PM";
				}
				
				if($F($('w_day')) >= 7)
				{
					time = ipb.lang['every_day_at_time'];
					time = time.sub("%s",timeOfDay);
				}
				else//if($F($('w_day')) < 7)
				{
					time = ipb.lang['every_x_at_time'];
					time = time.sub("<#TIME#>",timeOfDay);
					time = time.sub("<#DAY#>",ipb.lang['dayweek'+$F($('w_day'))]);
				}
			}
			
			var message = ipb.lang['wage_message_2'];
			message = message.sub("<#TIME#>",time);
			
			$('paymentInfo').value = message;
			
			return;
		}
		
		$('paymentInfo').value = ipb.lang['wage_bad_input'];
	},
	setTime: function(event)
	{
		var date = new Date();
		var minute = date.getMinutes();
		if(minute < 10)
		{
			minute = "0"+minute;
		}
		
		$('w_when').value = (date.getUTCMonth()+1)+"/"+date.getUTCDate()+"/"+date.getUTCFullYear()+" "+date.getUTCHours()+":"+date.getUTCMinutes();
		
		ibMarketWageForm.updateWage();
		
		$('w_when').focus();
	}
}

ibMarketWageForm.init();
